package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    @Reference
    private HouseService houseService;
    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private HouseUserService houseUserService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        //拿到前端传过来的数据封装并回显
        Map<String, Object> filters = this.getFilters(request);
        model.addAttribute("filters",filters);
        //根据封装参数进行查询
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page",page);
        //查询需要的小区列表
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        //查询前端需要的数据字典的下拉表  用途地区之类的
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floorList = dictService.findListByDictCode("floor");
        model.addAttribute("floorList",floorList);
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructureList);
        List<Dict> directionList = dictService.findListByDictCode("direction");
        model.addAttribute("directionList",directionList);
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        model.addAttribute("decorationList",decorationList);
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        model.addAttribute("houseUseList",houseUseList);
        return "house/index";
    }

    @RequestMapping("/create")
    public String create(Model model){
        //需要把一些下拉栏的数据带过去,也就是小区跟区域用途那些
        //查询需要的小区列表
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        //查询前端需要的数据字典的下拉表  用途地区之类的
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floorList = dictService.findListByDictCode("floor");
        model.addAttribute("floorList",floorList);
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructureList);
        List<Dict> directionList = dictService.findListByDictCode("direction");
        model.addAttribute("directionList",directionList);
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        model.addAttribute("decorationList",decorationList);
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        model.addAttribute("houseUseList",houseUseList);
        //跳转到添加页面
        return "house/create";
    }

    @RequestMapping("/save")
    public String save(House house){
        this.houseService.insert(house);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id , Model model){
        //根据ID查询到房屋信息并放入共享域
        House house = this.houseService.getById(id);
        model.addAttribute("house",house);
        //还有必要的那七个字段也放入
        //查询需要的小区列表
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        //查询前端需要的数据字典的下拉表  用途地区之类的
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floorList = dictService.findListByDictCode("floor");
        model.addAttribute("floorList",floorList);
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructureList);
        List<Dict> directionList = dictService.findListByDictCode("direction");
        model.addAttribute("directionList",directionList);
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        model.addAttribute("decorationList",decorationList);
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        model.addAttribute("houseUseList",houseUseList);
        //跳转到编辑页面
        return "house/edit";
    }

    //用于把更新后的数据保存的handler
    @RequestMapping("/update")
    public String update(House house){
        this.houseService.insert(house);
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        this.houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable Long id,@PathVariable int status){
        //封装下这两个数据,直接交给修改函数
        House house = new House();
        house.setId(id);
        house.setStatus(status);
        houseService.update(house);
        return "redirect:/house";
    }

    @RequestMapping("/{id}")
    public String detail(@PathVariable Long id,Model model){
        //查询当前id(传入的是房源的ID)的房屋信息
        House house = this.houseService.getById(id);
        model.addAttribute("house",house);
        //查询当前房源的小区信息
        Community community = this.communityService.getById(house.getCommunityId());
        model.addAttribute("community",community);
        //获取当前房源的房源图片信息
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        model.addAttribute("houseImage1List",houseImage1List);
        //获取当前房源的房产图片信息
        List<HouseImage> houseImage2List = houseImageService.findList(id, 2);
        model.addAttribute("houseImage2List",houseImage2List);
        //获取当前房源的经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        model.addAttribute("houseBrokerList",houseBrokerList);
        //获取当前房源的房东信息
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(id);
        model.addAttribute("houseUserList",houseUserList);
        return "house/show";
    }
}
